package cn.yeziji.forum.listener;

import cn.yeziji.forum.common.key.WebsocketCacheKey;
import cn.yeziji.forum.factory.websocket.WebsocketOperationFactory;
import cn.yeziji.forum.factory.websocket.enums.WebsocketFactoryOption;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * 文章redis监听
 *
 * @author gzkemays
 * @since 2021/12/22 23:49
 */
@Slf4j
// @Component
public class ChatRoomRedisListener extends KeyExpirationEventMessageListener {
  public ChatRoomRedisListener(RedisMessageListenerContainer listenerContainer) {
    super(listenerContainer);
  }

  @Override
  public void onMessage(Message message, byte[] pattern) {
    log.info("监听 message --> {}", message.toString());
    String key = message.toString();
    // 监听聊天室数据缓存
    if (key.contains(WebsocketCacheKey.PRIVATE_TO_AND_FROM)) {
      WebsocketOperationFactory.operation(WebsocketFactoryOption.CHAT_ROOM_EXPIRED, null, key);
    }
    super.onMessage(message, pattern);
  }
}
